<?php


namespace app\user\controller;


use app\common\Check;
use think\Db;
use think\facade\Request;

class Setting extends LoggingBase
{
    /**
     * 重設密碼
     */
    public function ResetPassword()
    {
        return $this->mb_Display('修改密码', 'ResetPassword');
    }

    /**
     * 基本信息
     */
    public function UserInfo()
    {
        $User = array(
            'Nick'=>$this->m_User->Nick,
            'Icon'=>$this->m_User->Icon,
            'Sex'=>$this->m_User->Sex,
            'WeChat'=>$this->m_User->WeChat,
            'QQ'=>$this->m_User->QQ,
            'Phone'=>$this->m_User->Phone,
            'Email'=>$this->m_User->Email,
            'Remark'=>$this->m_User->Remark
        );
        $this->assign($User);
        $Css = array(
            '<link href="/layui/css/layui.css" rel="stylesheet"/>'
        );
        return $this->mb_Display('基本信息', 'UserInfo', 'user_info', $Css);
    }

    /**
     * 提交用戶信息
     */
    public function mb_SubmitUserInfo()
    {
        $UserID = $this->m_User->ID;
        $Icon = Request::has('Icon') ? Request::param('Icon') : '';
        $Nick = Request::has('Nick') ? Request::param('Nick') : '';
        $Sex = Request::has('Sex') ? Request::param('Sex') : '';
        $WeChat = Request::has('WeChat') ? Request::param('WeChat') : '';
        $QQ = Request::has('QQ') ? Request::param('QQ') : '';
        $Phone = Request::has('Phone') ? Request::param('Phone') : '';
        $Email = Request::has('Email') ? Request::param('Email') : '';
        $Remark = Request::has('Remark') ? Request::param('Remark') : '';

        if($Nick == '') mb_OutMessage(ErrCode_Toast,'请输入姓名');
        if($Phone == '') mb_OutMessage(ErrCode_Toast,'请输入电话号码');
        if($Email == '') mb_OutMessage(ErrCode_Toast,'请输入电子邮箱');

        if(!Check::IsChinaPhone($Phone))mb_OutMessage(ErrCode_Toast,'请输入中国大陆的手机号码');
        if(!Check::IsEmail($Email))mb_OutMessage(ErrCode_Toast,'请输入合法的电子邮箱');

        $User = Db::table('user')->where("IsDeleted='0' AND Phone='$Phone'")->find();
        if ($User != null && $User['ID'] != $UserID) mb_OutMessage(ErrCode_Toast, '该电话号码已被注册');


        Db::startTrans();
        try {
            $Data = array(
                'ID' => $UserID,
                'Name' => $Phone,
                'Nick' => $Nick,
                'Sex' => $Sex,
                'WeChat' => $WeChat,
                'QQ' => $QQ,
                'Phone' => $Phone,
                'Email'=>$Email,
                'Remark' => $Remark,
                'Icon'=>$Icon
            );
            Db::table('user')->update($Data);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal,'保存成功');
    }

    /**
     * 提交重设密码
     */
    public function mb_SubmitResetPassword()
    {
        $OldPass = Request::has('OldPass') ? addslashes(Request::param('OldPass')) : '';
        $NewPass = Request::has('NewPass') ? addslashes(Request::param('NewPass')) : '';

        if($this->m_User->BasePass != $OldPass){
            mb_OutMessage(ErrCode_Toast, '旧密码不正确');
        }

        Db::startTrans();
        try {
            $this->m_User->EncPass = md5($NewPass);
            $this->m_User->BasePass = $NewPass;
            $this->m_User->save();

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '保存成功');
    }
}